package com.developer.platform.application.query.assembler;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.developer.platform.application.query.RoleQueryManager;
import com.developer.platform.application.query.dto.CurrUserDto;
import com.developer.platform.application.query.dto.ResponseModel;
import com.developer.platform.application.query.dto.RouteDto;
import com.developer.platform.application.query.dto.UserAndRoleDto;
import com.developer.platform.application.query.dto.UserListDto;
import com.developer.platform.application.query.dto.UserRoleDto;
import com.developer.platform.domain.User;
import com.developer.platform.domain.security.AuthorizationInfo;
import com.developer.platform.domain.security.UserDetails;
import com.developer.platform.infrastructure.enums.MenuEnum;
import com.veeker.core.utils.TreeUtil;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import org.springframework.stereotype.Component;

/**
 * @author ：qiaoliang
 * @date ：2020-11-09
 */
@Component
public class UserAssembler {

    private final RoleQueryManager roleQueryManager;

    public UserAssembler(RoleQueryManager roleQueryManager) {
        this.roleQueryManager = roleQueryManager;
    }

    public UserAndRoleDto toUserAndRoleDto(User user){
        if(Objects.nonNull(user)){
            UserAndRoleDto userAndRoleDto = new UserAndRoleDto();
            BeanUtil.copyProperties(user, userAndRoleDto);
            UserRoleDto userRoleDto = roleQueryManager.userRoleByUserId(user.getId());
            userAndRoleDto.setList(userRoleDto.getList());
            userAndRoleDto.setRoleIds(userRoleDto.getRoleIds());
            return userAndRoleDto;
        }
        return null;
    }


    public ResponseModel<UserListDto> toUserListDtoPage(IPage<User> page){
        return new ResponseModel<>(page.getRecords().stream().map(user -> {
            UserListDto userListDto = new UserListDto();
            BeanUtil.copyProperties(user, userListDto);
            return userListDto;
        }).collect(Collectors.toList()), (int)page.getTotal());
    }


    public CurrUserDto toCurrUserDto(UserDetails authUserDetails, List<AuthorizationInfo> list){
        CurrUserDto currUserDto = new CurrUserDto(authUserDetails.getId(), authUserDetails.getName(), authUserDetails.getAvatar());
        currUserDto.setMenus(TreeUtil.buildByRecursive(list.stream()
                .filter(authMenu -> MenuEnum.C.equals(authMenu.getType())||MenuEnum.M.equals(authMenu.getType()))
                .map(authMenu -> {
                    RouteDto menuDto = new RouteDto(authMenu.getId(), authMenu.getParentId());
                    menuDto.setComponent(authMenu.getComponent());
                    menuDto.setRedirect(authMenu.getType().equals(MenuEnum.M)?"noRedirect":null);
                    menuDto.setName(authMenu.getName());
                    menuDto.setPath(authMenu.getPath());
                    menuDto.setMeta(new RouteDto.Meta(authMenu.getTitle(),authMenu.getIcon()));
                    return menuDto;
                }).collect(Collectors.toList()),0L));
        return currUserDto;
    }

}
